/**
 * Copyright &copy; 2012-2014 nplus1 All rights reserved.
 */
package com.gamma.nplus1.common.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * Cache工具类
 * @author ThinkGem
 * @version 2013-5-29
 */
public class CacheUtils {
	
	private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));

	private static final String SYS_CACHE = "sysCache";
	
	/** 
     *  
     * 是否会死亡 
     */  
  
    public static boolean ETERNAL = false;  
  
    /** 
     *  
     * 缓存的间歇时间 
     */  
  
    public static int TIMETOIDLESECONDS = 600;  
  
    /** 
     *  
     * 存活时间(默认一天) 
     */  
  
    public static int TIMETOlIVESECONDS = 86400;  

	/**
	 * 获取SYS_CACHE缓存
	 * @param key
	 * @return
	 */
	public static Object get(String key) {
		return get(SYS_CACHE, key);
	}
	
	/**
	 * 写入SYS_CACHE缓存
	 * @param key
	 * @return
	 */
	public static void put(String key, Object value) {
		put(SYS_CACHE, key, value);
	}
	
	/**
	 * 从SYS_CACHE缓存中移除
	 * @param key
	 * @return
	 */
	public static void remove(String key) {
		remove(SYS_CACHE, key);
	}
	
	/**
     * 从SYS_CACHE设置存活时间
     * @param key
     * @return
     */
	public static void set(String key, Object value, int timeToLiveSeconds) throws Exception {  
        set(SYS_CACHE,key,value,timeToLiveSeconds);
    }
	
	/**
	 * 获取缓存
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public static Object get(String cacheName, String key) {
		Element element = getCache(cacheName).get(key);
		return element==null?null:element.getObjectValue();
	}

	/**
	 * 写入缓存
	 * @param cacheName
	 * @param key
	 * @param value
	 */
	public static void put(String cacheName, String key, Object value) {
		Element element = new Element(key, value);
		getCache(cacheName).put(element);
	}

	/**
	 * 从缓存中移除
	 * @param cacheName
	 * @param key
	 */
	public static void remove(String cacheName, String key) {
		getCache(cacheName).remove(key);
	}
	
	/**
	 * 获得一个Cache，没有则创建一个。
	 * @param cacheName
	 * @return
	 */
	private static Cache getCache(String cacheName){
		Cache cache = cacheManager.getCache(cacheName);
		if (cache == null){
			cacheManager.addCache(cacheName);
			cache = cacheManager.getCache(cacheName);
			cache.getCacheConfiguration().setEternal(true);
		}
		return cache;
	}

	public static CacheManager getCacheManager() {
		return cacheManager;
	}
	
	/** 
     *  
     * 向指定容器中设置值 
     *  
     * @param cacheName 
     *            容器名 
     *  
     * @param key 
     *            键 
     *  
     * @param value 
     *            值 
     *  
     * @param timeToIdleSeconds 
     *            间歇时间 
     *  
     * @param timeToLiveSeconds 
     *            存活时间 
     *  
     * @return 真 
     *  
     */  
	public static void set(String cacheName, String key, Object value, int timeToLiveSeconds) throws Exception {  
	    Cache myCache = getCache(cacheName);
        Element element = new Element(key, value);
        element.setTimeToLive(timeToLiveSeconds);
        myCache.put(element); 
    }  
}
